package com.itheima.system.service.impl;
import com.itheima.common.entity.ResultCode;
import com.itheima.common.exception.IhrmException;
import com.itheima.system.mapper.PermissionMapper;
import com.itheima.system.pojo.Permission;
import com.itheima.system.pojo.PermissionApi;
import com.itheima.system.pojo.PermissionMenu;
import com.itheima.system.pojo.PermissionPoint;
import com.itheima.system.service.PermissionService;
import com.itheima.common.utils.BeanMapUtils;
import com.itheima.common.utils.IdWorker;
import com.itheima.common.utils.PermissionConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author IntelliJ IDEA
 * @date 2020/10/26  15:43
 */
@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private PermissionMapper permissionMapper;


    /**
     * 新增权限
     */
    @Override
    public void addPermission(Map<String, Object> map) throws Exception {
        //1. 生成id
        String id=String.valueOf(idWorker.nextId());
        //2. map对象转换为Bean对象
        Permission permission = BeanMapUtils.mapToBean(map, Permission.class);
        permission.setId(id);
        //3. 获取权限类型
        Integer type = permission.getType();
        switch (type){
            case PermissionConstants.PERMISSION_MENU:
                //3.1 保存菜单权限
                PermissionMenu permissionMenu = BeanMapUtils.mapToBean(map, PermissionMenu.class);
                permissionMenu.setId(id);
                permissionMapper.addPermissionMenu(permissionMenu);
                break;
            case PermissionConstants.PERMISSION_POINT:
                //3.2 保存按钮权限
                PermissionPoint permissionPoint = BeanMapUtils.mapToBean(map, PermissionPoint.class);
                permissionPoint.setId(id);
                permissionMapper.addPermissionPoint(permissionPoint);
                break;
            case PermissionConstants.PERMISSION_API:
                //3.3 保存api权限
                PermissionApi permissionApi = BeanMapUtils.mapToBean(map, PermissionApi.class);
                permissionApi.setId(id);
                permissionMapper.addPermissionApi(permissionApi);
                break;
            default:
                throw new IhrmException(ResultCode.FAIL);
        }
        //4. 保存权限本身
        permissionMapper.addPermission(permission);
    }

    /**
     * 修改权限
     */
    @Override
    public void updatePermission(Map<String, Object> map) throws Exception {
        //1. 将map转为Bean对象
        Permission permission = BeanMapUtils.mapToBean(map, Permission.class);
        //2. 获取type
        Integer type = permission.getType();
        switch (type){
            case PermissionConstants.PERMISSION_MENU:
                //2.1 修改菜单权限
                PermissionMenu permissionMenu = BeanMapUtils.mapToBean(map, PermissionMenu.class);
                permissionMenu.setId(permission.getId());
                permissionMapper.updatePermissionMenu(permissionMenu);
                break;
            case PermissionConstants.PERMISSION_POINT:
                //2.2 修改按钮菜单
                PermissionPoint permissionPoint = BeanMapUtils.mapToBean(map, PermissionPoint.class);
                permissionPoint.setId(permission.getId());
                permissionMapper.updatePermissionPoint(permissionPoint);
                break;
            case PermissionConstants.PERMISSION_API:
                //2.3 修改api权限
                PermissionApi permissionApi = BeanMapUtils.mapToBean(map, PermissionApi.class);
                permissionApi.setId(permission.getId());
                permissionMapper.updatePermissionApi(permissionApi);
                break;
            default:
                throw new IhrmException(ResultCode.FAIL);
        }
        //3. 修改权限
        permissionMapper.updatePermission(permission);
    }

    /**
     * 删除权限
     */
    @Override
    public void deletePermission(String id) {
         //1. 根据id删除权限
        Permission permission=permissionMapper.findPermissionById(id);
         //2. 获取type
        Integer type = permission.getType();
        switch (type){
            case PermissionConstants.PERMISSION_MENU:
                //2.1 删除菜单权限
                permissionMapper.deletePermissionMenu(permission.getId());
                break;
            case PermissionConstants.PERMISSION_POINT:
                //2.2 删除按钮权限
                permissionMapper.deletePermissionPoint(permission.getId());
                break;
            case PermissionConstants.PERMISSION_API:
                //2.3 删除api权限
                permissionMapper.deletePermissionApi(permission.getId());
                break;
             default:
                 throw new IhrmException(ResultCode.FAIL);
        }
        //3. 删除权限
        permissionMapper.deletePermission(id);
    }

    /**
     * 查询列表权限
     *
     * 如果type:0 查询菜单+按钮，如果为1，查询全部，type为其他，抛异常
     * enVisible: 0 查询所有saas平台的最高权限   1 查询企业的权限
     * pid 父id
     */
    @Override
    public List<Permission> findAllPermission(Map<String, Object> map) {
        Integer type=null;
        String enVisible=null;
        String pid=null;
        //1. 获取type,enVisible,pid的值，参数健壮性判断
        if(map.get("type")!=null){
            type=Integer.parseInt(map.get("type").toString());
        }
        if(map.get("enVisible")!=null){
            enVisible=(String) map.get("enVisible");
        }
        if(map.get("pid")!=null){
            pid=(String) map.get("pid");
        }
        //2. 查询权限列表
        List<Permission> permissionList=permissionMapper.findAllPermission(type,enVisible,pid);
        return permissionList;
    }

    /**
     * 根据ID查询权限
     */
    @Override
    public Map<String, Object> findPermissionById(String id) {
        //1. 根据ID查询权限
        Permission permission = permissionMapper.findPermissionById(id);
        //2. Bean 转换为Map
        Map<String, Object> resultMap = BeanMapUtils.beanToMap(permission);
        //3. 获取类型
        Integer type = permission.getType();
        Map<String,Object> map=new HashMap<String,Object>(0);
        switch (type){
            case PermissionConstants.PERMISSION_MENU:
                map=permissionMapper.finPermissionMenuByType(permission.getId());
                break;
            case PermissionConstants.PERMISSION_POINT:
                map = permissionMapper.findPermissionPointByType(permission.getId());
                break;
            case PermissionConstants.PERMISSION_API:
                map = permissionMapper.findPermissionApiByType(permission.getId());
                break;
            default:
                throw new IhrmException(ResultCode.FAIL);
        }
        if(map!=null){
            resultMap.putAll(map);
        }
        return resultMap;
    }

    /**
     * 查询对应角色下所有的权限id的集合
     */
    @Override
    public Set<String> findPermIdsByRoleId(String id) {
        return permissionMapper.findPermIdsByRoleId(id);
    }
}
